<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="指标归属" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入指标归属"  v-decorator="[ 'indexGroup', validatorRules.indexGroup]">
            <a-select-option value="1">本部各部门</a-select-option>
            <a-select-option value="2">社区&医院</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="指标类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入指标类型"  v-decorator="[ 'indexType', validatorRules.indexType]">
            <a-select-option value="0">累计</a-select-option>
            <a-select-option value="1">考核</a-select-option>
            <a-select-option value="2">权重</a-select-option>
            <a-select-option value="3">加减分项</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="指标编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'indexCode', validatorRules.indexCode]" placeholder="请输入指标编码"></a-input>
        </a-form-item>
        <a-form-item label="指标名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'indexName', validatorRules.indexName]" placeholder="请输入指标名称"></a-input>
        </a-form-item>
        <a-form-item label="指标值编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'indexValueCode', validatorRules.indexValueCode]" placeholder="请输入指标值编码"></a-input>
        </a-form-item>
        <a-form-item label="指标值名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'indexValueName', validatorRules.indexValueName]" placeholder="请输入指标值名称"></a-input>
        </a-form-item>
        <a-form-item label="指标值排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'indexValueOrder', validatorRules.indexValueOrder]" placeholder="请输入指标值排序"></a-input>
        </a-form-item>
        <a-form-item label="是否可编辑" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入是否可编辑"  v-decorator="[ 'indexValueEdit', validatorRules.indexValueEdit]">
            <a-select-option value="0">否</a-select-option>
            <a-select-option value="1">是</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="是否展示" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入是否展示"  v-decorator="[ 'indexValueShow', validatorRules.indexValueShow]">
            <a-select-option value="0">否</a-select-option>
            <a-select-option value="1">是</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="指标值格式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="请输入指标值格式"  v-decorator="[ 'indexValueFormat', validatorRules.indexValueFormat]">
            <a-select-option value="1">整型</a-select-option>
            <a-select-option value="2">浮点型</a-select-option>
            <a-select-option value="3">百分比</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: "TkIndexInfoModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          indexGroup: {rules: [
            {required: true, message: '请输入指标归属!'},
          ]},
          indexType: {rules: [
            {required: true, message: '指标类型!'},
          ]},
          indexCode: {rules: [
            {required: true, message: '请输入指标编码!'},
          ]},
          indexName: {rules: [
            {required: true, message: '请输入指标名称!'},
          ]},
          indexValueName: {rules: [
            {required: true, message: '请输入指标值名称!'},
          ]},
          indexValueCode: {rules: [
            {required: true, message: '请输入指标值编码!'},
          ]},
          indexValueOrder: {rules: [
            {required: true, message: '请输入排序!'},
          ]},
          indexValueEdit: {rules: [
            {required: true, message: '请输入是否可编辑!'},
          ]},
          indexValueShow: {rules: [
            {required: true, message: '请输入是否展示!'},
          ]},
          indexValueFormat: {rules: [
            {required: true, message: '请输入指标值格式!'},
          ]},
        },
        url: {
          add: "/tkindexinfo/tkIndexInfo/add",
          edit: "/tkindexinfo/tkIndexInfo/edit",
        }
      }
    },
    created () {
      
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'indexGroup','indexType','indexCode','indexValueCode','indexName','indexValueName','indexValueOrder','indexValueEdit','indexValueShow','indexValueFormat'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            if(formData.indexGroup == '1'){
              formData.indexGroupName='本部各部门';
            }else{
              formData.indexGroupName='社区&医院';
            }
            if(formData.indexType=='0'){
              formData.indexTypeName ='累计';
            }else if(formData.indexType=='1'){
              formData.indexTypeName ='考核';
            }else if(formData.indexType=='2'){
              formData.indexTypeName ='权重';
            }else if(formData.indexType=='3'){
              formData.indexTypeName ='加减分项';
            }
            
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'indexGroup','indexType','indexCode','indexValueCode','indexName','indexValueName','indexValueOrder','indexValueEdit','indexValueShow','indexValueFormat'))
      },

      
    }
  }
</script>